//
//  DDTabBarController.swift
//  doudou_weibo
//
//  Created by apple on 2016/11/12.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class DDTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        addchildViewControllers()
        
        //修改tabbarVC的tabBar为自定义的tabBar
        let ddtabBar = DDTabBar()
        self.setValue(ddtabBar, forKey: "tabBar")
        
        ddtabBar.composeClosure = {
            print(#function,"点击了撰写按钮")
        }
        
        
        
    }


    //自定义方法创建4个tabBarButton
    private func addchildViewControllers() {
        
        addChildViewController(controller: DDHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(controller: DDMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(controller: DDDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(controller: DDProfileTableViewController(), title: "我", imageName: "tabbar_profile")

    }
    
    
    
    private func addChildViewController(controller: UIViewController, title: String, imageName: String) {
        //标题
        controller.title = title
        //图片
        controller.tabBarItem.image = UIImage(named: imageName)
        controller.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //文字
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.darkGray], for: .normal)
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orange], for: .selected)
        controller.tabBarItem.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 10)], for: .normal)
        //标签
        controller.tabBarItem.badgeValue = "9"
        controller.tabBarItem.badgeColor = #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1)
        //如果想取消标签就设置标签为nil
//        controller.tabBarItem.badgeValue = nil
        //设置文字和图片的距离
        controller.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
        //设置图片的偏移----这里不需要偏移,所以设置为0
        controller.tabBarItem.imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        
        //创建navigationVC
        let navigationController = DDNavigationController(rootViewController: controller)
        //将navigationVC添加到tabBarVC上
        addChildViewController(navigationController)
        
    }
    
    
    

}











